<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="草稿箱" name="first">
        <el-table :data="applyList" border fit highlight-current-row>
          <el-table-column type="expand" label="内容" width="80">
            <template slot-scope="props">
              <el-form label-position="left" inline class="post-del-expand">
                <el-form-item>
                  <dl v-html="props.row.content"></dl>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="序号" width="100" align="center">
            <template slot-scope="scope">
              {{ (applyPage.apage - 1) * applyPage.alimit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="title" label="标题" />
          <el-table-column prop="createTime" label="发帖时间" />
          <el-table-column label="操作" width="300" align="center">
            <template slot-scope="scope">
              <el-button type="success" size="mini" @click="setPrime(scope.row.id)">重要</el-button>
              <el-button type="primary" size="mini" @click="setNormal(scope.row.id)">普通</el-button>
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="deletePost(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination :current-page="applyPage.apage" :page-size="applyPage.alimit" :total="applyPage.atotal"
          :page-sizes="[5, 10, 20, 50]" style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper" @current-change="handleListCurrent"
          @size-change="handleListSize" />
      </el-tab-pane>
      <el-tab-pane label="已发布" name="second">
        <el-table :data="historyList" border fit highlight-current-row>
          <el-table-column type="expand" label="内容" width="80">
            <template slot-scope="props">
              <el-form label-position="left" inline class="post-del-expand">
                <el-form-item>
                  <dl v-html="props.row.content"></dl>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="序号" width="100" align="center">
            <template slot-scope="scope">
              {{ (historyPage.hpage - 1) * historyPage.hlimit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="title" label="标题" />
          <el-table-column prop="createTime" label="发帖时间" />
          <el-table-column prop="operateTime" label="发布时间" />
          <el-table-column label="状态" width="100">
            <template slot-scope="scope">
              <el-tag v-show="scope.row.state === -1" type="danger">拒绝发布</el-tag>
              <el-tag v-show="scope.row.state === 0" type="info">等待审核</el-tag>
              <el-tag v-show="scope.row.state === 1" type="primary">普通发帖</el-tag>
              <el-tag v-show="scope.row.state === 2" type="success">精品发帖</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="300" align="center">
            <template slot-scope="scope">
              <el-button type="succe" size="mini" @click="setPrime(scope.row.id)">重要</el-button>
              <el-button type="primary" size="mini" @click="setNormal(scope.row.id)">普通</el-button>
              <el-button type="danger" size="mini" @click="setDeny(scope.row.id)">下架</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination :current-page="historyPage.hpage" :page-size="historyPage.hlimit" :total="historyPage.htotal"
          :page-sizes="[5, 10, 20, 50]" style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange"
          @size-change="handleSizeChange" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import invitationApi from '@/api/invitation'

export default {
  data() {
    return {
      role: this.$store.getters.roles,
      activeName: 'first',
      applyList: null,
      applyPage: {
        apage: 1,
      alimit: 5,
      atotal: 0
      },
      historyList: null,
      historyPage: {
        hpage: 1,
      hlimit: 5,
      htotal: 0
      },
    }
  },
  created() {
    this.applyPage = {
      apage: 1,
      alimit: 5,
      atotal: 0
    }
    this.historyPage = {
      hpage: 1,
      hlimit: 5,
      htotal: 0
    }
  },
  mounted() {
    if (this.activeName === 'first') {
      this.getList();
    }
    else if (this.activeName === 'second') {
      this.getHistory();
    }
  },
  methods: {
    // 获得列表
    getList() {
      const page = this.applyPage.apage
      const limit = this.applyPage.alimit
      invitationApi.getExamine(page, limit)
        .then(response => {
          this.applyList = response.data.item
          this.applyPage.atotal = response.data.total
        })
        .catch(() => {
        })
    },
    // 获得处理过的记录
    getHistory() {
      const page = this.historyPage.hpage
      const limit = this.historyPage.hlimit
      invitationApi.getApprove(page, limit)
        .then(response => {
          this.historyList = response.data.item
          this.historyPage.atotal = response.data.total
        })
        .catch(() => {
        })
    },
    setPrime(id) {
      this.$confirm('是否设置为精华发帖?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 点击确定，执行then方法
        // 调用删除的方法
        invitationApi.prime(id)
          .then(response => {
            // 提示信息
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            // 回到列表页面
            if (this.activeName === 'first') {
              this.getList();
            }
            else if (this.activeName === 'second') {
              this.getHistory();
            }
          })
      }) // 点击取消，执行catch方法
    },
    setDeny(id) {
      this.$confirm('是否下架发帖?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 点击确定，执行then方法
        // 调用删除的方法
        invitationApi.deny(id)
          .then(response => {
            // 提示信息
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            // 回到列表页面
            if (this.activeName === 'first') {
              this.getList();
            }
            else if (this.activeName === 'second') {
              this.getHistory();
            }
          })
      }) // 点击取消，执行catch方法
    },
    setNormal(id) {
      this.$confirm('是否设置为普通发帖?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 点击确定，执行then方法
        // 调用删除的方法
        invitationApi.pass(id)
          .then(response => {
            // 提示信息
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            // 回到列表页面
            if (this.activeName === 'first') {
              this.getList();
            }
            else if (this.activeName === 'second') {
              this.getHistory();
            }
          })
      }) // 点击取消，执行catch方法
    },
    deletePost(id) {
      this.$confirm('删除该项发帖申请吗，删除后将无法恢复记录', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        let arr = [];
        arr.push(id);
        invitationApi.delete(arr)
          .then(response => {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            if (this.activeName === 'first') {
              this.getList();
            }
            else if (this.activeName === 'second') {
              this.getHistory();
            }
          })
      }).catch(() => { })
    },
    // 分页
    handleListCurrent(val) {
      this.applyPage.apage = val
      this.getList()
    },
    handleListSize(val) {
      this.applyPage.alimit = val
      this.applyPage.apage = 1
      this.getList()
    },
    // 分页
    handleCurrentChange(val) {
      this.historyPage.hpage = val
      this.getHistory()
    },
    handleSizeChange(val) {
      this.historyPage.hlimit = val
      this.historyPage.hpage = 1
      this.getHistory()
    },

    handleClick(val) {
      if (val.name === 'first') {
        this.getList()
      } else {
        this.getHistory()
      }
    },
  }
}
</script>
<style lang="scss">
.post-del-expand {
  padding-left: 10%;
  padding-right: 10%;

  .el-input {
    margin-bottom: 20px;
  }

}
</style>



